from django.http import HttpResponse
from django.shortcuts import render, redirect
# from app01.requires_auth import requires_auth
from django.views import View
# from django.db import models
from app01 import models


class Register(View):
    """
    用户注册
    """

    def get(self, request):
        pass

    def post(self, request):
        user = request.POST.get("username")
        pwd = request.POST.get("password")
        models.UserInfo.objects.all()
        models.UserInfo.objects.create(username=user, password=pwd)


def login(request):
    """
    用户登录
    :param request:
    :return:
    """
    if request.method == "GET":
        return render(request, "login.html")
    else:
        user = request.POST.get("username")
        pwd = request.POST.get("password")
        if user == "admin" and pwd == "123456":
            obj = redirect("/layout/")
            obj.set_cookie("ticket", "909009090")
            return obj
        else:
            return render(request, "login.html")


def layout(request):
    tk = request.COOKIES.get("ticket")
    if not tk:
        return redirect("/login/")
    return render(request, "layout.html")


class Index(View):
    """
    分页，获取用户数据
    """

    def get(self, request):
        pass

    def post(self, request):
        a = models.UserInfo.objects.all()
        return a


